<template>
  <div>
    <el-card shadow="hover">
      <div slot="header">
        <el-button type="primary" icon="el-icon-plus" @click="showAdd">添加</el-button>
      </div>
      <el-table :data="trademarkList" row-key="id" border>
        <el-table-column label="序号" type="index" width="80" align="center" />

        <el-table-column prop="tmName" label="品牌名称" />

        <el-table-column label="品牌LOGO">
          <template v-slot="scope">
            <el-image :src="scope.row.logoUrl" style="width: 100px; height:60px;" />
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template v-slot="{ row, $index }">
            <el-button title="修改" type="warning" size="mini" icon="el-icon-edit-outline"></el-button>
            <el-button title="删除" type="danger" size="mini" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination style="margin-top: 20px; text-align: center;" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[2, 5, 10]" :page-size="100"
        layout="prev, pager, next, jumper, ->, sizes, total" :total="total">
      </el-pagination>
      <!-- 对话框 -->
      <el-dialog title="修改品牌" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="品牌名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="品牌LOGO" :label-width="formLabelWidth">
            <el-upload class="avatar-uploader" :action="`${BASE_URL}/admin/product/fileUpload`"
              :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import trademarkList from '@/api/trademark'
export default {
  name: 'Trademark',
  data() {
    return {
      trademarkList: [],
      currentPage: 1,
      pageSize: 2,
      total: 100,
      gridData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }],
      dialogFormVisible: false,   //是否显示对话框
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px',
      imageUrl:'',
      BASE_URL:''
    }
  },


  mounted() {
    this.getTrademarkList(1, 2)
    this.BASE_URL = process.env.VUE_APP_BASE_API
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.getTrademarkList(this.currentPage, val)
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getTrademarkList(val, this.pageSize)
      console.log(`当前页: ${val}`);
    },
    async getTrademarkList(pages, size) {
      const res = await trademarkList(pages, size)
      console.log(res)
      this.trademarkList = res.data.records
      this.total = res.data.total    //总共条数
    },
    showAdd() {     //点击添加
      this.dialogFormVisible = true
    },
    handleAvatarSuccess(res, file) {   //对话框
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPNG) {
        this.$message.error('上传头像图片只能是 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isPNG && isLt2M;
    }
  }
}
</script>

<style lang="less" scoped></style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}</style>
